package model.process;

import com.sun.management.OperatingSystemMXBean;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import rie.model.CPU;
import rie.model.Memoire;
import rie.model.Processus;
import rie.model.SystemeInfo;

public class DisplayProcess {

    public static List<Processus> ListProcess() throws IOException {
        List<Processus> tasklist = new ArrayList();
        String[] command = new String[]{"tasklist", "/fo", "csv", "/nh"};
        Process process = Runtime.getRuntime().exec(command);
        BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = "";
        while ((line = input.readLine()) != null) {
            if (!line.equals("")) {
                line = line.replaceAll("\"", "");
                Processus processus = new Processus();
                String[] taskAttributes = null;
                taskAttributes = line.split(",");
                processus.setName(taskAttributes[0] == null ? "" : taskAttributes[0]);
                processus.setPid(taskAttributes[1] == null ? "" : taskAttributes[1]);
                processus.setMemoryUsage(taskAttributes[4] == null ? "" : taskAttributes[4] + taskAttributes[5] == null ? "" : taskAttributes[5]);
                tasklist.add(processus);
            }
        }
        return tasklist;

    }

    public static long getFreeRAM() {
        long RAM;
        OperatingSystemMXBean os = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        RAM = os.getFreePhysicalMemorySize() / (1024 * 1024);
        return RAM;
    }

    public static int getProco() throws IOException {
        int proco = 0;
        String[] command = new String[]{"wmic", "cpu", "get", "loadpercentage"};
        Process process = Runtime.getRuntime().exec(command);
        BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = "";
        line = input.readLine();
        line = input.readLine();
        line = input.readLine().trim();
        proco = Integer.valueOf(line).intValue();
        return proco;
    }

    public static SystemeInfo getSystem() throws IOException {
        SystemeInfo retour = null;
        if (System.getProperty("os.name").contains("Windows")) {
            retour = new SystemeInfo(new CPU(getProco()), new Memoire(getFreeRAM()), ListProcess());
        } else {
            System.out.println("OS différent de windows non supporté");
        }

        return retour;
    }

    public static void main(String args[]) throws IOException {
        List<Processus> processes = null;
        int cpu = 0;
        long mem = 0;
        if (System.getProperty("os.name").contains("Windows")) {
            processes = ListProcess();
            cpu = getProco();
            mem = getFreeRAM();
        } else {
            System.out.println("OS différent de windows non supporté");
        }
        String result = "";

        Iterator<Processus> it = processes.iterator();
        int i = 0;
        while (it.hasNext()) {
            Processus p = it.next();
            result += p.getName() + "|" + p.getPid() + ",";
            i++;
            if (i == 10) {
                result += "\n";
                i = 0;
            }
        }
        System.out.println(result);
        System.out.println("Utilisation processeur: " + cpu + "%.");
        System.out.println("Memoire libre: " + mem + " Mo.");
    }
}
